//
//  AppDelegate.m
//  WeiBo17
//
//  Created by teacher on 15/8/16.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "AppDelegate.h"
#import "IWTabBarController.h"
#import "IWNewFeatureCtrl.h"
#import "IWOAuthViewCtrl.h"
#import "IWAccount.h"
#import "IWAccountTool.h"
#import "IWWindowTool.h"
#import "UIWindow+Extension.h"
#import <FIR/FIR.h>

#define key_ShortVersion @"key_ShortVersion"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{

    [FIR handleCrashWithKey:@"70430cfe45c6790eaa56c9b37b96222f"];

    //1.初始化一个window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    //与本地保存的版本号进入对比,如果本地没有版本号或者本地的版本号低于当前app的版本号,就显示新特性

    //本地保存的版本号
    NSString* localShortVersionStr = [[NSUserDefaults standardUserDefaults] objectForKey:key_ShortVersion];

    //取出当前app的版本号
    NSString* currentShortVersionStr = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    //判断当前本地保存的版本号为空,或者本地保存的版本号小于当前app的版本号,就直接进入到新特性页面
    if (!localShortVersionStr || [localShortVersionStr compare:currentShortVersionStr] == NSOrderedAscending) {

        //进入新特性控制器之前,保存一下当前app的版本号,以便下次进入的时候判断
        [[NSUserDefaults standardUserDefaults] setObject:currentShortVersionStr forKey:key_ShortVersion];
        [[NSUserDefaults standardUserDefaults] synchronize];

        //加载新特性页面
        self.window.rootViewController = [IWNewFeatureCtrl new];
    }else {
        
//        [IWWindowTool switchRootViewCtrl];
        [self.window swithRootViewCtrl];
        
//        //判断一下帐号是否过期,如果过期又得跳转登录页面
//        IWAccount* account = [IWAccountTool account];
//
////        //判断一下是否过期.
////        //求出当前帐号过期的时间
////        NSDate* date = [account.createDate dateByAddingTimeInterval:account.expires_in];
////        //拿着过期的时间与当前的时间进行对比
////        NSDate* currentDate = [NSDate date];
////
////        //    timeIntervalSinceDate -->可以直接计算出两个时间的差值(秒)
////        //    compare               -->可以对比出两个时间的早晚
////
////        if ([currentDate compare:date] != NSOrderedAscending) {
////            //代表过期
////            account = nil;
////        }
//
//        
//        
//        
//        if (account) {
//            //进入主页
//            self.window.rootViewController = [IWTabBarController new];
//        }else {
//            //代表帐号不存在或者过期,进入登录页面
//            self.window.rootViewController = [IWOAuthViewCtrl new];
//        }
    }

    //3.让显示并成为主窗口
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication*)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication*)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication*)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication*)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication*)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
